package com.shanyu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageInfo;
import com.shanyu.dao.IItemCommentDao;
import com.shanyu.dao.IItemDao;
import com.shanyu.dao.IItemSkuDao;
import com.shanyu.dao.IMediaDao;
import com.shanyu.dao.IMemberDao;
import com.shanyu.dao.ISkuDao;
import com.shanyu.dto.ItemCommentDto;
import com.shanyu.dto.ItemDto;
import com.shanyu.eo.ItemCommentEo;
import com.shanyu.eo.ItemEo;
import com.shanyu.eo.ItemSkuEo;
import com.shanyu.eo.MediaEo;
import com.shanyu.eo.MemberEo;
import com.shanyu.eo.SkuEo;
import com.shanyu.service.IItemCommentService;
import com.shanyu.utils.IdGen;

@Service
public class ItemCommentService implements IItemCommentService{
	
	@Autowired
	private IItemCommentDao itemCommentDao;
	
	@Autowired
	private IItemDao itemDao;
	
	@Autowired
	private IItemSkuDao itemSkuDao;
	
	@Autowired
	private ISkuDao skuDao;
	
	@Autowired
	private IMediaDao mediaDao;
	
	@Autowired
	private IMemberDao memberDao;

	public PageInfo getItemComment(String itemId, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		PageInfo res = new PageInfo(null);
		ItemCommentEo query = new ItemCommentEo();
		query.setStatus(1);
		if(!StringUtils.isEmpty(itemId)){
			query.setObjectId(itemId);
		}
		PageInfo page = itemCommentDao.page(query, pageNum, pageSize);
		BeanUtils.copyProperties(page, res);
		
		res.setList(cloneList(page.getList()));
		return res;
	}
	
	
	private List<ItemCommentDto> cloneList(List<ItemCommentEo> list){
		List<ItemCommentDto> res = new ArrayList<ItemCommentDto>();
		
		if(!CollectionUtils.isEmpty(list)){
			for(ItemCommentEo eo : list){
				ItemCommentDto dto = new ItemCommentDto();
				BeanUtils.copyProperties(eo, dto);
				dto.setReply(getReply(eo.getId()));
				dto.setItemDto(getDetail(eo.getObjectId()));
				dto.setMedia(getMediaList(eo.getObjectId()));
				dto.setMember(getMemberInfo(eo.getMemberId()));
				res.add(dto);
			}
		}
		
		return res;
	}
	
	
	private ItemDto getDetail(String itemId) {
		// TODO Auto-generated method stub
		ItemDto dto = new ItemDto();
		
		ItemEo eo = itemDao.getById(itemId);
		if(eo != null){
			BeanUtils.copyProperties(eo, dto);
		}
		dto.setSkuList(getSku(itemId));
		return dto;
	}
	
	private ItemCommentEo getReply(String id){
		return itemCommentDao.getById(id);
	}
	
	private List<SkuEo> getSku(String itemId){
		ItemSkuEo query = new ItemSkuEo();
		query.setItemId(itemId);
		List<ItemSkuEo> list = itemSkuDao.select(query);
		List<String> skuIds = new ArrayList<String>();
		if(!CollectionUtils.isEmpty(list)){
			for(ItemSkuEo eo : list){
				skuIds.add(eo.getSkuId());
			}
		}
		if(!CollectionUtils.isEmpty(skuIds)){
			return skuDao.selectBySkuIds(skuIds);
		}
		return null;
	}
	
	
	
	
	private List<MediaEo> getMediaList(String itemId){
		MediaEo query = new MediaEo();
		query.setObjectId(itemId);
		return mediaDao.select(query);
	}
	
	private MemberEo getMemberInfo(String memberId){
		return memberDao.getById(memberId);
	}


	public void insertItemComment(String itemId, String skuId, String coentent, String memberId,String imgUrls,
			float score) {
		// TODO Auto-generated method stub
		String id = IdGen.creatUUID();
		ItemCommentEo eo = new ItemCommentEo();
		eo.setComment(coentent);
		eo.setId(id);
		eo.setMemberId(memberId);
		eo.setObjectId(itemId);
		eo.setSkuId(skuId);
		eo.setScore(score);
		itemCommentDao.insert(eo);
		
		String[] url = imgUrls.split(",");
		for(String str : url){
			MediaEo media = new MediaEo();
			media.setId(IdGen.creatUUID());
			media.setImgUrl(str);
			media.setObjectId(id);
			mediaDao.insert(media);
		}
	}
	
	

}
